<!DOCTYPE html>
<html>
    <head>
        <style>
            canvas{
                width: 270vh;
                height: 90vh;
            }
        </style>
        <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    </head>
    <body>
        <canvas width="3000" height="1000"></canvas>
        <button onclick="sco()">For Once</button>
        <button onclick="scoI = setInterval('sco()',100);">Start</button>
        <button onclick="clearInterval(scoI);">Stop</button>
    </body>
    <script src="js/main.js"></script>
    <script src="js/Hole.js"></script>
    <script src="js/HallWay.js"></script>
    <script src="js/ClassRoom.js"></script>
    <script>
        /**
        @version 0.2
        */
        var scoI;
        var hallwT = HallWay.newHallWay(6,70,1,0,1,testNewClassRoom(4,50),8,0);
        function HallWayRun(){
            let people = hallwT.people;
            let holeMaxX = hallwT.width;
            let holeMaxY = hallwT.length;
            let classrooms = hallwT.classRooms;
            let margin = hallwT.classRoomMargin;
            let leftW = hallwT.leftW;
            for(let i in people){
                let peo = people[i];
                if(peo == null) continue;
                if(peo.y>holeMaxY || peo.y<-1) {
                    people[i] = null;continue;//记得改
                }
                let outSpace = 1;//允许逃生的最小距离
                if(peo.x>holeMaxX-3){//阻止了门口同学的逃生
                    let usY = leftW+1;
                    for(let i in classrooms){
                        if(Math.abs(usY-1-peo.y)<outSpace) classrooms[i].left1 = true;
                        if(Math.abs(usY+1-peo.y)<outSpace) classrooms[i].left2 = true;
                        usY+=structure.classWidth;
                        if(Math.abs(usY-1-peo.y)<outSpace) classrooms[i].right1 = true;
                        if(Math.abs(usY+1-peo.y)<outSpace) classrooms[i].right2 = true;
                        usY+=margin;
                    }
                }
                //v1
                // for(let ii in people){
                //     if(i==ii) continue;
                //     peoI = people[ii];
                //     if(distance(peo,peoI) <2){//发生碰撞
                //             //peo.vx=(peoI.vx<0?1:-1)*Math.max(Math.abs(peo.vx),0.5);
                //             peo.vx=0;
                //             if(peo.y<peoI.y)peo.vy=0;
                //     }
                // }
                let accuracy = 16;
                let acuR = Math.PI/accuracy/2;
                let purposeLeft = peo.purpose==hallwT.leftConnection;
                let correctByPurpose = purposeLeft?Math.PI:0;//对于反向者
                let minsit=Math.PI/2+correctByPurpose;let minWt=doubleMax;
                let vabs = 1//Math.floor(Math.min(Math.max(0.1/minWt,0.1),1)*Math.random()*10000)/10000;
                for(let sitk =acuR+correctByPurpose;sitk<=Math.PI+correctByPurpose;sitk+=acuR){
                    let res = 0;
                    let pd = {x:peo.x+vabs*Math.cos(sitk),y:peo.y+Math.sin(sitk)}
                    for(let ii in people){
                        let peoI = people[ii];
                        if(people[ii] == null) continue;
                        if(i == ii || (purposeLeft?(peoI.y>= peo.y):(peoI.y<= peo.y))) continue;
                        let dist = distance(pd,peoI);
                        //if(i==5) console.log(sitk/Math.PI*180,dist);
                        if(dist<=0.5) res+=doubleMax;
                        if(dist>=4) continue;
                        res+=1/dist;
                    }
                    if(res ==0){//轨道修正
                        minsit = Math.PI/2 +correctByPurpose;
                        minWt = Math.random()*0.5;
                        break;
                    }
                    res+=1/(Math.max(0.000000001,pd.x))*0.5;
                    res+=1/(Math.max(0.000000001,Math.abs((holeMaxX-pd.x))))*0.5;
                    
                    //if(i==5)console.log(i+" "+sitk+" "+res)
                    if(res>=0 && res<minWt){
                        minsit = sitk;
                        minWt = res;
                    }
                    
                }
                //Math.sqrt(peoI.vx*peoI.vx+peoI.vy*peoI.vy);
                //console.log(minWt);
                let oldSita =sitaGiver(peo.vx,peo.vy);
                console.log(minsit*180/Math.PI);
                minsit = oldSita+(minsit-oldSita)*0.75;
                vabs*=Math.random()*Math.min(1.5,10/minWt);
                peo.vx = vabs*Math.cos(minsit);
                peo.vy = vabs*Math.sin(minsit);
                peo.x+=peo.vx;
                if(peo.x<1) peo.x=1;
                if(peo.x>holeMaxX-1)peo.x=holeMaxX-1;
                peo.y+=peo.vy; 
                // if(peo.x<=1){
                //     peo.vx=0.5;
                // }
                // if(peo.x>=7){
                //     peo.vx=0.5;
                // }
                
            }
            let usY = leftW+1;
            for(let i in classrooms){
                //console.log("?")
                if((!classrooms[i].left1) && classrooms[i].population>0) {
                    newPeo(people,holeMaxX-1,usY-1,0,0,classrooms[i].purpose);classrooms[i].population--;}
                if((!classrooms[i].left2) && classrooms[i].population>0) {
                    newPeo(people,holeMaxX-1,usY+1,0,0,classrooms[i].purpose);classrooms[i].population--;}
                usY+=structure.classWidth;
                if((!classrooms[i].right1) && classrooms[i].population>0) {
                    newPeo(people,holeMaxX-1,usY-1,0,0,classrooms[i].purpose);classrooms[i].population--;}
                if((!classrooms[i].right2) && classrooms[i].population>0) {
                    newPeo(people,holeMaxX-1,usY+1,0,0,classrooms[i].purpose);classrooms[i].population--;}
                classrooms[i].left1 = classrooms[i].left2 = classrooms[i].right1 = classrooms[i].right2 = false;
                usY+=margin;
            }
        }
        function displayHallWay(){
            let people = hallwT.people;
            let holeMaxX = hallwT.width;
            let holeMaxY = hallwT.length;
            let classrooms = hallwT.classRooms;
            let margin = hallwT.classRoomMargin;
            let leftW = hallwT.leftW;
            let lefx = 200,upy=200,wid=100;
            canvas.beginPath();
            canvas.strokeStyle = "black";
            drawLine(lefx,upy,lefx+wid*holeMaxY/holeMaxX,upy);
            drawLine(lefx,upy+wid,lefx+wid*holeMaxY/holeMaxX,upy+wid);
            let usY = leftW+1;
            // canvas.strokeStyle = "gey";
            canvas.fillStyle = "black";
            for(let i in classrooms){
                canvas.fillRect(lefx+wid*usY/holeMaxX,upy+wid,wid*(structure.classWidth)/holeMaxX,20);
                canvas.font="20px Georgia";
                canvas.fillText(classrooms[i].population,lefx+wid*usY/holeMaxX,upy+wid+40);
                usY+=structure.classWidth+margin;
            }
            // canvas.fillStyle = "red";
            // for(let i in people){
            //     if(people[i] == null) continue;
            //     canvas.beginPath();
            //     canvas.arc(lefx+wid*people[i].x/holeMaxX,upy+wid*people[i].y/holeMaxX,wid*1/holeMaxX+0.3,0,2*Math.PI);
            //     canvas.fill();
            // }
            for(let i in people){
                if(people[i] == null) continue;
               canvas.fillStyle = people[i].color;
                canvas.beginPath();
                canvas.arc(lefx+wid*people[i].y/holeMaxX,upy+wid*people[i].x/holeMaxX,wid*1/holeMaxX,0,2*Math.PI);
                drawLine(lefx+wid*people[i].y/holeMaxX,upy+wid*people[i].x/holeMaxX,
                lefx+wid*(people[i].y+people[i].vy*10)/holeMaxX,upy+wid*(people[i].x+people[i].vx*10)/holeMaxX)
                canvas.fill();
            }
            canvas.beginPath();
        }
        function newPeo(people,x,y,vx,vy,purpose){
            people.push({
                x:x,
                y:y,
                vx:vx,
                vy:vy,
                purpose:purpose,
                color:"#"+Math.floor(Math.random()*900000)
            });
        }
        
        function summon(){//forTest
            let a;//测试xy
            let undone = true;
            while(undone){
                a={x:0+Math.random()*4,y:0+Math.random()*20}
                undone = false;
                for(let i in people){
                    if(people[i] == null) continue;
                    if(distance(a,people[i]) <2 || a.x<=1 || a.x>=holeMaxX-1) {
                        undone = false;break;
                    }
                }
            }
            newPeo(a.x,a.y,-0.5+Math.random(),-0.5+Math.random());
        }
        function sco(){
            canvas.fillStyle = "white";
            canvas.fillRect(0,0,3000,1000);
            HallWayRun();
            displayHallWay();
        }
        // for(let i =0;i<60;i++){
        //     summon();
        // }
        displayHallWay();
        
        //setInterval("summon()",100)

    </script>
</html>